#!/bin/sh
# ***************************************************************************
# bootstrap : Set up config.mak
# ***************************************************************************
# Copyright (C) 2003-2009 the VideoLAN team
# $Id$
#
# Authors: Christophe Massiot <massiot@via.ecp.fr>
#          Derk-Jan Hartman <hartman at videolan dot org>
#          Felix Paul Kühne <fkuehne at videolan dot org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
# ***************************************************************************

if test "$#" -gt "2" -o "$1" = "-h" -o "$1" = "--help" ; then
    echo "Usage: $0 [host [distro]]" >&2
    echo "  Prepare config.mak and distro.mak file." >&2
    exit 1
fi

LANG=C
export LANG
set -e
set +x

BUILD=`gcc -dumpmachine`
if test "$#" -ge "1"; then
    HOST="$1"
else
    HOST="$BUILD"
fi

DISTRO=
if test "$#" = "2"; then
    echo "Forcing distro.mak to use $2.mak"
    DISTRO="$2"
    HOST="$1"
fi

rm -f config.mak
{
    echo "# Automatically generated by bootstrap."
    echo "# Make changes if you know what you're doing."
} > config.mak

rm -f distro.mak
{
    echo "# Automatically generated by bootstrap"
    echo "# Make changes if you know what you're doing."
} > distro.mak

if test "$HOST" != "$BUILD"; then
    CC="${HOST}-gcc"
    CXX="${HOST}-g++"
    LD="${HOST}-ld"
    RANLIB="${HOST}-ranlib"
    AR="${HOST}-ar"
    STRIP="${HOST}-strip"
fi

case $HOST in
    *powerpc*|*ppc*)
         echo "ARCH=ppc" >> config.mak
     ;;
    *86_64*)
          echo "ARCH=x86_64" >> config.mak
     ;;
    *86*)
          echo "ARCH=i386" >> config.mak
     ;;
esac

# Check the HAVE_{OS}
case $HOST in
    *darwin*)
        echo "HAVE_DARWIN_OS = 1" >> config.mak
        echo "HAVE_BSD = 1" >> config.mak
    ;;
    *linux*)
        echo "HAVE_LINUX = 1" >> config.mak
    ;;
    *bsd*)
        echo "HAVE_BSD = 1" >> config.mak
    ;;
    *wince*)
        echo "HAVE_WINCE = 1" >> config.mak
    ;;
    *beos*)
        echo "HAVE_BEOS = 1" >> config.mak
    ;;
esac

case $HOST in
    ppc-darwin)
        # Mac OS X < 10.4
        echo "Your version of Mac OS X is too old!" >&2
        echo "Compiling VLC requires 10.5.x or later" >&2
        exit 1
    ;;
    *-apple-darwin8)
        echo "Your version of Mac OS X is too old!" >&2
        echo "Compiling and running VLC requires 10.5.x or later" >&2
        exit 1
    ;;
    powerpc-apple-darwin9)
        SDK_TARGET=10.5
        echo "SDK_TARGET = ${SDK_TARGET}" >> config.mak
        echo "ENVP = MACOSX_DEPLOYMENT_TARGET=${SDK_TARGET}" >> config.mak
        echo "MACOSX_SDK = /Developer/SDKs/MacOSX10.5.sdk" >> config.mak
        echo "HAVE_DARWIN_9 = 1" >> config.mak
        HAVE_DARWIN_9=1
        HAVE_DARWIN_32=1
        CFLAGS_TUNING=" -arch ppc -mtune=G4"
        EXTRA_CFLAGS=" -D\${ENVP} -isysroot \${MACOSX_SDK} -mmacosx-version-min=\${SDK_TARGET}"
        EXTRA_LDFLAGS=" -arch ppc -isysroot \${MACOSX_SDK} -mmacosx-version-min=\${SDK_TARGET} -Wl,-syslibroot,\${MACOSX_SDK}"
        CC="/Developer/usr/bin/gcc-4.2"
        CXX="/Developer/usr/bin/g++-4.2"
        LD="ld -arch ppc -syslibroot \${MACOSX_SDK} -mmacosx-version-min=\${SDK_TARGET}"
        echo "PATH = /bin:/usr/bin:/usr/local/bin:" >> config.mak
        DISTRO=darwin
        if test -e /Developer/SDKs; then
            echo "SDKs found. Everything is fine." >&2
        else
            echo "ERROR:\nYour Developer Tools' SDKs were not found.\nYou need to add extra symbolic links to /Developer to achieve correctly\nbuilt contribs.\nHave a look at the OSX-Compile-HOWTO for details." >&2
            exit 1
        fi
    ;;
    i686-apple-darwin*)
        SDK_TARGET=10.5
        MIN_LD_VERSION_WITH_TEXT_RELOCATION=81
        echo "SDK_TARGET = ${SDK_TARGET}" >> config.mak
        echo "HAVE_DARWIN_OS_ON_INTEL = 1" >> config.mak
        echo "HAVE_DARWIN_9 = 1" >> config.mak
        HAVE_DARWIN_9=1
        if test `ld -v 2> /dev/stdout | sed -E 's/.*ld64-([0-9]+).*/\1/'` -lt ${MIN_LD_VERSION_WITH_TEXT_RELOCATION}; then
            echo "ERROR: Your ld version is unable to compile VLC.\nUpdate to Xcode 3.1 or higher." >&2
            exit 1
        fi
        echo "ENVP = MACOSX_DEPLOYMENT_TARGET=${SDK_TARGET}" >> config.mak
        echo "MACOSX_SDK = /Developer/SDKs/MacOSX10.5.sdk" >> config.mak
        HAVE_DARWIN_32=1
        CFLAGS_TUNING=" -march=prescott -mtune=generic -arch i386 -m32"
        EXTRA_CFLAGS=" -D\${ENVP} -isysroot \${MACOSX_SDK} -mmacosx-version-min=\${SDK_TARGET}"
        EXTRA_LDFLAGS=" -arch i386 -isysroot \${MACOSX_SDK} -Wl,-syslibroot,\${MACOSX_SDK} -mmacosx-version-min=\${SDK_TARGET}"
        CC="/Developer/usr/bin/gcc-4.2"
        CXX="/Developer/usr/bin/g++-4.2"
        echo "PATH = /bin:/usr/bin:/usr/local/bin" >> config.mak
        DISTRO=darwin
        if test -e /Developer/SDKs; then
            echo "SDKs found. Everything is fine." >&2
        else
            echo "ERROR:\nYour Developer Tools' SDKs were not found.\nYou need to add extra symbolic links to /Developer to achieve correctly\nbuilt contribs.\nHave a look at the OSX-Compile-HOWTO for details." >&2
            exit 1
        fi
        if test $HOST = "i686-apple-darwin10"; then
            echo "HAVE_DARWIN_10 = 1" >> config.mak
        fi
    ;;
    x86_64-apple-darwin*)
        SDK_TARGET=10.5
        echo "SDK_TARGET = ${SDK_TARGET}" >> config.mak
        echo "HAVE_DARWIN_OS_ON_INTEL = 1" >> config.mak
        echo "HAVE_DARWIN_9 = 1" >> config.mak
        echo "ENVP = MACOSX_DEPLOYMENT_TARGET=${SDK_TARGET}" >> config.mak
        echo "MACOSX_SDK = /Developer/SDKs/MacOSX${SDK_TARGET}.sdk" >> config.mak
        HAVE_DARWIN_64=1
        CFLAGS_TUNING=" -march=core2 -mtune=core2 -m64 -arch x86_64"
        echo "HAVE_DARWIN_64 = 1" >> config.mak
        EXTRA_CFLAGS=" -D\${ENVP} -isysroot \${MACOSX_SDK} -mmacosx-version-min=\${SDK_TARGET}"
        EXTRA_LDFLAGS=" -isysroot \${MACOSX_SDK} -Wl,-syslibroot,\${MACOSX_SDK} -mmacosx-version-min=\${SDK_TARGET}"
        echo "PATH = /bin:/usr/bin:/usr/local/bin" >> config.mak
        CC="/Developer/usr/bin/gcc-4.2"
        CXX="/Developer/usr/bin/g++-4.2"
        LD="ld"
        RANLIB="ranlib"
        AR="ar"
        STRIP="strip"
        DISTRO=darwin64
        if test -e /Developer/SDKs; then
            echo "SDKs found. Everything is fine." >&2
        else
            echo "ERROR:\nYour Developer Tools' SDKs were not found.\nYou need to add extra symbolic links to /Developer to achieve correctly\nbuilt contribs.\nHave a look at the OSX-Compile-HOWTO for details." >&2
            exit 1
        fi
        if test $HOST = "x86_64-apple-darwin10"; then
            echo "HAVE_DARWIN_10 = 1" >> config.mak
        fi
    ;;
    i586-pc-beos)
        if test -f /boot/beos/system/lib/libbind.so; then
            EXTRA_LDFLAGS=" -lbind -lsocket"
        else
            EXTRA_LDFLAGS=" -lnet"
        fi
        if test "$HOST" = "$BUILD"; then
#           Compiling on BeOS
            PATH=".:/boot/home/config/bin:/bin:/boot/apps"
            PATH="${PATH}:/boot/preferences:/boot/beos/apps"
            PATH="${PATH}:/boot/beos/preferences"
            PATH="${PATH}:/boot/develop/tools/gnupro/bin"
            echo "PATH = ${PATH}" >> config.mak
            LIBRARY_PATH="${LIBRARY_PATH}:%A/lib:/boot/home/config/lib"
            LIBRARY_PATH="${LIBRARY_PATH}:/boot/beos/system/lib"
            echo "LIBRARY_PATH = ${LIBRARY_PATH}" >> config.mak
        else
#           Cross compiling from Linux
            PATH="/opt/beos-tools/bin:/opt/cross-tools/bin"
            PATH="${PATH}:/usr/bin:/bin"
            echo "PATH = ${PATH}" >> config.mak
        fi
        cat src/Distributions/beos.mak >> distro.mak
    ;;
    *mingw32ce)
        echo "HAVE_WINCE = 1" >> config.mak
        echo "PKG_CONFIG_PATH = \$(PREFIX)/lib/pkgconfig" >> config.mak
        EXTRA_CPPFLAGS=" -D_WIN32_WCE=0x0500"
        DISTRO=wince
    ;;
    x86_64-*-mingw*)
        echo "HAVE_WIN64 = 1" >> config.mak
        echo "HAVE_WIN32 = 1" >> config.mak
        echo "PKG_CONFIG_PATH = \$(PREFIX)/lib/pkgconfig" >> config.mak
           EXTRA_CFLAGS="-O3"
        DISTRO=win64
    ;;
    *mingw32*)
        echo "HAVE_WIN32 = 1" >> config.mak
        echo "PKG_CONFIG_PATH = \$(PREFIX)/lib/pkgconfig" >> config.mak
        EXTRA_CFLAGS="-O3"
        DISTRO=win32
    ;;
    i686-pc-cygwin)
        echo "HAVE_WIN32 = 1" >> config.mak
        echo "HAVE_CYGWIN = 1" >> config.mak
        CC="gcc -mno-cygwin -isystem /usr/include/mingw"
        CXX="g++ -mno-cygwin -isystem /usr/include/mingw"
        HOST=`$CC -dumpmachine`
        EXTRA_CFLAGS=" -mno-cygwin -isystem /usr/include/mingw"
        EXTRA_CPPFLAGS=" -mno-cygwin -isystem /usr/include/mingw"
        EXTRA_LDFLAGS=" -mno-cygwin"
        echo "PKG_CONFIG_PATH = \$(PREFIX)/lib/pkgconfig" >> config.mak
        DISTRO=win32
    ;;
    arm-wince-pe)
        echo "PKG_CONFIG_PATH = \$(PREFIX)/lib/pkgconfig" >> config.mak
        EXTRA_CPPFLAGS=" -D_WIN32_WCE"
        DISTRO=wince
    ;;
    armeb-linux-uclibc)
        echo "HAVE_UCLIBC = 1" >> config.mak
        echo "HAVE_BIGENDIAN = 1" >> config.mak
        EXTRA_CFLAGS="-Os -march=armv5 -msoft-float"
    ;;
    arm-none-linux-gnueabi)
        if test -f /etc/maemo_version; then
            DISTRO=maemo
            echo 'HAVE_MAEMO = 1' >> config.mak
            EXTRA_CFLAGS=" -mcpu=cortex-a8 -mtune=cortex-a8 -march=armv7-a"
            EXTRA_CLFAGS="$EXTRA_CFLAGS -mfpu=neon"
            EXTRA_CFLAGS="$EXTRA_CFLAGS -mthumb -mthumb-interwork"
            echo 'HAVE_ISA_THUMB = 1' >> config.mak
        else
            EXTRA_CFLAGS="-msoft-float"
        fi
    ;;
    *86_64*linux*)
        EXTRA_CFLAGS=" -fPIC"
        EXTRA_CPPFLAGS=" -fPIC"
        EXTRA_LDFLAGS=" -L/usr/lib64"
        echo "LIBRARY_PATH = /usr/lib64" >> config.mak
        echo "PKG_CONFIG_PATH = /usr/lib64/pkgconfig" >> config.mak
        echo "PKG_CONFIG_LIBDIR = /usr/lib64/pkgconfig" >> config.mak
    ;;
esac

if test -z "${DISTRO}" -a "$HOST" = "$BUILD"; then
    if test -d "/usr/lib/pkgconfig"; then
        if test -z "$PKG_CONFIG_PATH"; then
            echo "PKG_CONFIG_PATH = /usr/lib/pkgconfig" >> config.mak
        fi
        if test -z "$PKG_CONFIG_LIBDIR"; then
            echo "PKG_CONFIG_LIBDIR = /usr/lib/pkgconfig" >> config.mak
        fi
    fi
    # Try to match distribution
    if test -f /etc/fedora-release; then
        DISTRO=fedora
    elif test -f /etc/maemo_version; then
        DISTRO=maemo
        echo 'HAVE_MAEMO = 1' >> config.mak
    elif test -f /etc/debian_version; then
        # NOTE: check for Debian *after* its derivatives
        DISTRO=debian
    fi
fi

# Default Unix-like systems
test -z "${DISTRO}" && DISTRO=unix

cat src/Distributions/"${DISTRO}".mak >> distro.mak

echo "BUILD = $BUILD" >> config.mak
echo "HOST = $HOST" >> config.mak

echo "PREFIX = `pwd`" >> config.mak

echo "CC = ${CC}" >> config.mak
echo "CXX = ${CXX}" >> config.mak
echo "LD = ${LD}" >> config.mak
echo "RANLIB = ${RANLIB}" >> config.mak
echo "AR = ${AR}" >> config.mak
echo "STRIP = ${STRIP}" >> config.mak
echo "EXTRA_CFLAGS = ${CFLAGS_TUNING} ${EXTRA_CFLAGS}" >> config.mak
echo "EXTRA_CPPFLAGS = ${EXTRA_CPPFLAGS} -isystem \$(PREFIX)/include" >> config.mak
echo "EXTRA_LDFLAGS = ${EXTRA_LDFLAGS}" >> config.mak
echo "EXTRA_PATH = ${EXTRA_PATH}" >> config.mak

if wget --version >/dev/null 2>&1; then
    echo "WGET = `which wget` -c --passive" >> config.mak
elif test -z `curl --version >/dev/null 2>&1`; then
    echo "WGET = `which curl` -L -O" >> config.mak
else
    echo "You need at least wget or curl to fetch the packages." >&2
    exit 1
fi

if svn --version >/dev/null 2>&1; then
    echo "SVN = `which svn`" >> config.mak
else
    echo "You do not have a subversion client in your PATH." >&2
fi

if git --version>/dev/null 2>&1; then
    echo "GIT = `which git`" >> config.mak
else
    echo "You do not have a Git client in your PATH." >&2
fi

if test -z "$CONTRIBS_RELEASE"; then
    echo "EXTRA_CFLAGS += -DNDEBUG" >> config.mak
    set +x
    echo
    echo "*****************************************************************"
    echo "* If you need contribs with all debug information, run this     *"
    echo "* line and compile the libraries on your own.                   *"
    echo "* CONTRIBS_RELEASE=no ./bootstrap                               *"
    echo "*****************************************************************"
fi

if test $HAVE_DARWIN_9; then
    set +x
    echo
    echo "*****************************************************************"
    echo "* We are using GCC-4.2 on OS X, so compilation WILL FAIL if it  *"
    echo "* is NOT installed. *"
    echo "*****************************************************************"
fi

if test $HAVE_DARWIN_32; then
    set +x
    echo
    echo "*****************************************************************"
    echo "* VLC will be compiled in 32bit mode.                           *" 
    echo "*                                                               *"
    echo "* Re-run with the x86_64-apple-darwin* argument to turn on      *" 
    echo "* 64bit compilation for Intel-based Macs, whereas * is either   *"
    echo "* 9 or 10 depending on your Darwin version.                     *"
    echo "* There is NO PPC64 support right now.                          *"
    echo "*****************************************************************"
fi

if test $HAVE_DARWIN_64; then
    set +x
    echo
    echo "*****************************************************************"
    echo "* VLC will be compiled in 64bit mode using the 10.5 SDK.        *" 
    echo "*****************************************************************"
    set -x
fi
